﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using System.Data.EntityClient;
using PBOffice.DAL;

namespace PBOffice.BLL
{
	public class DepartmentManager
	{

		#region 查询部门信息（根据机构编号）
		/// <summary>
		/// 查询部门信息（根据机构编号）
		/// </summary>
		/// <param name="organNo">机构编号</param>
		/// <returns></returns>
        public List<v_DepOrgan> ShowDepInfoByOrganNo(int organNo)
		{
			using (MyOfficeEntities myOffice = new MyOfficeEntities())
			{
				var department = from dep in myOffice.v_DepOrgan
								 where dep.OrganNo == organNo
								 select dep;                //查询部门信息
				return department.ToList();                //返回部门信息集合
			}
		}
		#endregion

        #region 查询部门信息（根据上级部门编号）
        /// <summary>
        /// 查询部门信息（根据上级部门编号）
        /// </summary>
        /// <param name="depNo">上级部门编号</param>
        /// <returns></returns>
        public List<v_DepOrgan> GetSubDepartments(int depNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var departs = from dep in myOffice.v_DepOrgan
                              where dep.DepParentNo == depNo
                              select dep;
                return departs.ToList();
            }
        }
        #endregion 

        #region 查询部门信息（根据部门编号)
        /// <summary>
        /// 查询部门信息（根据部门编号）
        /// </summary>
        /// <param name="depNo">部门编号</param>
        /// <returns></returns>
        public v_DepOrgan ShowDepInfoByDepNo(int depNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var dep = from d in myOffice.v_DepOrgan
                          where d.DepNo == depNo
                          select d;
                return dep.ToList().FirstOrDefault();
            }
        }
        #endregion

		#region 添加部门信息
		/// <summary>
		/// 添加部门信息
		/// </summary>
		/// <param name="d"></param>
		/// <returns></returns>
		public bool AddDepInfo(Department d,int organNo)
		{

			using (MyOfficeEntities myOffice = new MyOfficeEntities())
			{
				Department dep = new Department
				{
					DepNo = d.DepNo,
					DepId = d.DepId,
					DepDescribe = d.DepDescribe,
					DepCode = d.DepCode,
					DepParentNo = d.DepParentNo,
					DepStatus = d.DepStatus,
				};
                dep.Organ = myOffice.Organs.SingleOrDefault( organ => organ.OrganNo==organNo);    //所属的机构编号
				myOffice.Departments.AddObject(dep);     //添加部门信息
				return myOffice.SaveChanges() > 0;          //保存添加信息到数据库，保存成功，返回true，否则，返回false
			}
		}
		#endregion

		#region 修改部门信息（根据部门编号）
		/// <summary>
		/// 修改部门信息（根据部门编号）
		/// </summary>
		/// <param name="d"></param>
		/// <returns></returns>
        public bool ChangeDepInfo(Department d, int organNo)
		{
			using (MyOfficeEntities myOffice = new MyOfficeEntities())
			{
				var dep1 = from dpt in myOffice.Departments
						   where dpt.DepNo == d.DepNo
						   select dpt;                    //根据部门编号查询部门信息，
                Department dep = new Department();
                dep = dep1.ToList().FirstOrDefault();      //返回第一个元素
				dep.DepId = d.DepId;
				dep.DepDescribe= d.DepDescribe;
				dep.DepCode = d.DepCode;
				dep.DepStatus = d.DepStatus;
				dep.DepParentNo = d.DepParentNo;
                dep.Organ = myOffice.Organs.SingleOrDefault(organ => organ.OrganNo == organNo);
				return myOffice.SaveChanges() > 0;       //将修改后新的数据保存到数据库中，保存成功，返回true，否则，返回false            
			}
		}
		#endregion

		#region 删除部门信息（根据部门编号）
		/// <summary>
		/// 删除部门信息（根据部门编号）
		/// </summary>
		/// <param name="depNo"></param>
		/// <returns></returns>
		public bool DeleteDepInfo(int depNo)
		{
			using (MyOfficeEntities myOffice = new MyOfficeEntities())
			{
				var dep1 = from dpt in myOffice.Departments          //根据部门编号查询部门信息
						   where dpt.DepNo == depNo
						   select dpt;
				dep1.First();   //返回查询对象中的第一个元素
				Department dep = (Department)dep1;
				myOffice.Departments.DeleteObject(dep);     //删除部门信息
				return myOffice.SaveChanges() > 0;                     //保存删除后数据库中的数据，删除成功，返回true，否则，返回false             
			}
		}
		#endregion

		
	}
}
